#!/usr/bin/env python
# -*- coding: utf-8 -*-

from abc import ABCMeta, abstractmethod
from typing import Any

from cta.impl.action.picture.create_optimize2_account_log_profit_and_loss_rate_picture_action import \
    CreateOptimize2AccountLogProfitAndLossRatePictureAction
from cta.impl.action.picture.create_quant2_account_log_profit_and_loss_rate_picture_action import \
    CreateQuant2AccountLogProfitAndLossRatePictureAction
from cta.impl.action.transaction_date.common_transaction_date_action import CommonTransactionDateAction
from cta.impl.step.optimize2.create_optimize2_account_log_profit_and_loss_rate_picture_step import \
    CreateOptimize2AccountLogProfitAndLossRatePictureStep
from cta.impl.step.quant2.create_quant2_account_log_profit_and_loss_rate_picture_step import \
    CreateQuant2AccountLogProfitAndLossRatePictureStep
from cta.impl.step.maximum_drawdown_print_step import MaximumDrawDownStep
from cta.impl.action.print.maximum_drawdown_print_action import MaximumDrawDownPrintAction
from cta.impl.step.transaction_date_step import TransactionDateStep
from web.constants.datetime_format import DatetimeFormat
from web.domain.responsibility_chain_dto import ResponsibilityChainDto
from web.manager.log_manager import LogManager
from web.util.datetime_util import DatetimeUtil

Logger = LogManager.get_logger(__name__)

class AbstractStrategy(metaclass=ABCMeta):
    """
    抽象策略类
    """

    def __init__(self):
        super().__init__()

        # 定义步骤
        self.transaction_date_step = TransactionDateStep(CommonTransactionDateAction())
        self.maximum_drawdown_print_step = MaximumDrawDownStep(MaximumDrawDownPrintAction())
        self.create_quant2_account_log_profit_and_loss_rate_picture_step = CreateQuant2AccountLogProfitAndLossRatePictureStep(CreateQuant2AccountLogProfitAndLossRatePictureAction())
        self.create_optimize2_account_log_profit_and_loss_rate_picture_step = CreateOptimize2AccountLogProfitAndLossRatePictureStep(CreateOptimize2AccountLogProfitAndLossRatePictureAction())

    @abstractmethod
    def start(self):
        """
        开始
        """

        pass

    def exec(self, responsibility_chain_dto: ResponsibilityChainDto = None) -> Any:
        """
        执行
        """

        # 查询时间范围
        transaction_date_list: list = self.transaction_date_step.exec()

        if transaction_date_list is not None and len(transaction_date_list) > 0:
            for transaction_date in transaction_date_list:

                _transaction_date: str = DatetimeUtil.datetime_to_str(transaction_date, DatetimeFormat.Date_Format)
                if _transaction_date == '20210118':
                    pass

                Logger.info("交易日期为[%s]", transaction_date)

                self.responsibility_chain_begin_step.exec(ResponsibilityChainDto(transaction_date=_transaction_date))
                pass

        else:
            Logger.warning("查询时间范围为空")

    def print_max_drawdown(self):
        """
        打印最大回撤
        """

        Logger.info('打印最大回撤')

        self.maximum_drawdown_print_step.exec()

    def create_quant2_account_log_profit_and_loss_rate_picture(self):
        """
        根据表quant2_account_log中每个账号的总资金，生成折线图
        """

        Logger.info('根据表quant2_account_log中每个账号的总资金，生成折线图')

        self.create_quant2_account_log_profit_and_loss_rate_picture_step.exec()

    def create_optimize2_account_log_profit_and_loss_rate_picture(self):
        """
        根据表optimize2_account_log中每个账号的总资金，生成折线图
        """

        Logger.info('根据表optimize2_account_log中每个账号的总资金，生成折线图')

        self.create_optimize2_account_log_profit_and_loss_rate_picture_step.exec()